<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="configName">
        <el-date-picker
            v-model="queryParams.dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @keyup.enter="handleQuery"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="用户" prop="configName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="请假类型" align="center" prop="ftype">
        <template #default="scope">
          <span v-if="scope.row.ftype === 1">调休</span>
          <span v-else-if="scope.row.ftype === 2">年假</span>
          <span v-else-if="scope.row.ftype === 3">加班</span>
          <span v-else-if="scope.row.ftype === 4">事假</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="sd" />
      <el-table-column label="结束时间" align="center" prop="ed" />
      <el-table-column label="天数" align="center" prop="dayNum" />
      <el-table-column label="小时数" align="center" prop="hourNum" />
      <el-table-column label="状态" align="center" prop="fstatus">
        <template #default="scope">
          <el-tag v-if="scope.row.fstatus === 1" type="success">已确认</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pn"
        v-model:limit="queryParams.ps"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="ClockRecord">
import { queryHolidayList } from "@/api/system/config";
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import {addClockRecord, delClockRecord} from "../../../api/system/config.js";
const { proxy } = getCurrentInstance();
const { sys_yes_no } = proxy.useDict("sys_yes_no");
const configList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const isUserVis = ref(false);
const time = ref(false);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pn: 1,
    ps: 10,
    ftype: 2,
    dateRange: [],
    userName: undefined
  }
});

const { queryParams, form } = toRefs(data);

/** 查询参数列表 */
function getList() {
  loading.value = true;
  const DATA = {
    ...queryParams.value,
    st: queryParams.value.dateRange?.[0],
    et: queryParams.value.dateRange?.[1]
  }
  queryHolidayList(DATA).then(response => {
    configList.value = response.data.list
    total.value = response.data.total
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pn = 1;
  getList();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const idList = (row.recordId && [row.recordId]) || ids.value;
  proxy.$modal.confirm('是否确认删除参数编号为"' + idList + '"的数据项？').then(function () {
    return delClockRecord({idList});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 导出按钮操作 */
function handleExport() {
  const tHeader = [
    '用户名', '请假类型', '开始时间', '结束时间', '天数', '小时数', '状态',
  ]
  const filterVal = [
    'userName', 'ftype', 'sd', 'ed', 'dayNum', 'hourNum', 'fstatus',
  ]

  let list = configList.value
  const DATA = {
    ...queryParams.value,
    ps: 100000,
    st: queryParams.value.dateRange?.[0],
    et: queryParams.value.dateRange?.[1]
  }
  queryHolidayList(DATA).then(response => {
    let list = response.data.list
    list.map(res => {
      if(res.ftype === 1) {
        res.ftype = '调休'
      } else if(res.ftype === 2) {
        res.ftype = '年假'
      } else if(res.ftype === 3) {
        res.ftype = '加班'
      } else if(res.ftype === 4) {
        res.ftype = '事假'
      }
      if(res.fstatus === 1) {
        res.fstatus = '已确认'
      }
    })
    const data = formatJson(filterVal, list)
    export_json_to_excel(tHeader, data, '请假记录表')
  });

}

getList();
</script>
